Ext.define('BenBen.modules.system.organization.DepartmentTree', {
    extend : 'Ext.tree.Panel',
    rootVisible: false,
    useArrows: true,
    frame: true,
    layout : 'border',
    border : false,
    height : 540,
    autoScroll : true,
	initComponent : function() {
		var me = this;
		var deptId = 0;
		
		var store = Ext.create('Ext.data.TreeStore', {
			fields: ['deptId','deptName'],
	        proxy: {
	            type: 'ajax',
	            url: 'system/department/tree'
	        }
	    });

        Ext.apply(this,{
			store : store,
			idField : 'deptId',
            displayField: 'deptName',
        	dockedItems: [{
                xtype: 'toolbar',
                items: [{
                    text: '刷新缓存',
                    handler: function(){
                    	store.load({
                    		params : {
                    			isReflesh : true
                    		}
                    	});
                    }
                },{
                    text: '同步微信',
                    handler: function(){
                    	if(deptId != 0){
                    		Ext.Ajax.request({
                                url: 'system/department/synchOriginzation',
                                method: 'PUT',
                                params : {
                                	deptId : deptId
                                },
                                callback: function (options, success, response) {
                                    if (success) {
                                        if (Ext.decode(response.responseText).success) {
                                        	Ext.Msg.confirm('成功','同步数据成功');
                						} else {
                							Ext.Msg.confirm('失败',Ext.decode(response.responseText).info);
                						}
                                    } else {
                                        Ext.Msg.confirm('失败','同步数据失败');
                                    }
                                }
                            });
                    	}
                    }
                }]
            }]
		});
       
        me.on("itemclick",function(view,record,item,index,e){
//    		alert("点击的节点ID是："+record.get('deptId')+",文字是："+record.raw.deptName);
        	deptId = record.get('deptId');
    		if(me.userStore){
    			me.userStore.load({
    				params : {
    					start : 0,
    					limit : 30,
    					departmentId : deptId
    				}
    			});
    		}
        });

		this.callParent(arguments);
	}
});